import '../../assets/js/jq/jquery';
import '../../assets/js/moor/moor';
import '../../assets/js/m7Util';
import '../../assets/js/moortools';
import '../../assets/js/m7PhoneBar';
import '../../assets/js/objectDeclare';
import '../../assets/js/recordError';
import '../../assets/js/m7Monitor';
import '../../assets/js/crypto/md5.min';
import '../../assets/js/crypto/base64.min';
import router from '../../router/index';

const state = {
  sinToken: '',   //拨打电话唯一标识
  is_dial: true,   //拨打按钮是否可点击
  is_hang_up: false,   //挂断按钮是否可点击
  is_calling: false,   //是否显示呼叫中弹框
  is_deal_promit: false,   //是否显示未接通弹框
  is_not_call_button: true,   //未接通弹框中的保存按钮是否可点击
  is_switch: false,   //是否显示不能切换状态弹框
  is_link: false,   //是否是正在通话中
  selected_visited_purpose: [],   //选中的拜访目的
  doctorId: '',   //电话拨通成功之后存储医生id
  selected_product_id: '', //拨打电话选中的产品id
  loading: false,
  supplement: false,
  //扩招外呼
  enrollmentExpansion: false,
  //是否系统挂断
  systemHangUp: false,
  type: '',
}

const mutations = {
  //拨打电话
  callPhone(state, phone) {
    let setInt = null;
    let callPhone = moorCall.Phone.prototype.dialout;
    callPhone(phone, '');
    setInt = setInterval(() => {
      if (window.successInfo.Event) {
        window.successInfo.Event.forEach(item => {
          if (item.Event === "ChannelStatus") {
            state.sinToken = item.Data.CallSheetID ? item.Data.CallSheetID : '';
            if (item.ChannelStatus === "Ring") {  //修改拨打按钮，挂断按钮样式
              state.is_dial = false;  //拨打按钮不可点击
              state.is_hang_up = true;  //挂机按钮可点击
              // state.is_calling = true;   //显示呼叫中弹框
            } else if (item.ChannelStatus === "Up") {    //本人已接通电话成功，提示呼叫中
              //显示呼叫中弹框
              // state.is_calling = true;
            } else if (item.ChannelStatus === "Link") {   //对方已接通电话，双方通话中
              //跳转至通话中，隐藏呼叫中弹框
              state.is_calling = false;
              state.is_link = true;   //正在通话中
              state.loading = false;
              state.supplement = false;
              state.enrollmentExpansion = true
              state.systemHangUp = false;
              state.is_hang_up = true;  //挂机按钮可点击
              if (state.type == 1) {
                router.push('/calling')
              } else if (state.type == 2) {
                state.hang_up_button = true;
              }
            } else if (item.ChannelStatus === "Hangup") {
              clearInterval(setInt);
              //关闭呼叫中弹框
              state.is_calling = false;
              //拨打按钮可点击
              state.is_dial = true;
              //挂机按钮不可点击
              state.is_hang_up = false;
              //正在通话中设置为false
              state.is_link = false;
              state.loading = false;
              state.hang_up_button = true;
              state.systemHangUp = true;
              //notDeal->未接通挂断  dealing->已接通挂断
              if (window.hangStatus === "notDeal" && state.sinToken) {
                //显示未接通弹框
                state.is_deal_promit = true;
                state.is_not_call_button = true;
              }
            }
          }
        });
      }
    }, 100);
  },
  //挂断电话
  hang() {
    let hang = moorCall.Phone.prototype.hangup;
    hang();
  }
}

export default {
  namespaced: true,
  state,
  mutations
}
